package com.weixin.paleituo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.bean.UserMyHomeListBean;
import com.weixin.paleituo.eventbus.UseMyHomeAttrEvent;
import com.weixin.paleituo.eventbus.UseMyHomeCheckPosition;
import com.weixin.paleituo.eventbus.UserMyHomeNumberEvent;
import com.weixin.paleituo.util.StringUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.NumberButton;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author yichao
 * Time  2018/4/26 10:57
 * Dest  应用到我家
 */

public class UseMyHomeRecycleViewAdapter extends RecyclerView.Adapter<UseMyHomeRecycleViewAdapter.UserMyHomeListBeanViewHolder> {
    private Context mContext;
    private List<UserMyHomeListBean> mUserMyHomeListBeans;

    public UseMyHomeRecycleViewAdapter(Context context, List<UserMyHomeListBean> userMyHomeListBeans) {
        mContext = context;
        mUserMyHomeListBeans = userMyHomeListBeans;
    }

    @Override
    public UserMyHomeListBeanViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        return new UserMyHomeListBeanViewHolder(UIUtils.inflate(mContext, R.layout.layout_item_use_myhome));
    }

    @Override
    public void onBindViewHolder(UserMyHomeListBeanViewHolder holder, final int position) {

        final UserMyHomeListBean userMyHomeListBean = mUserMyHomeListBeans.get(position);

        final boolean check = userMyHomeListBean.isCheck();

        if (check){
            holder.mCkLayoutItemUseMyhome.setBackgroundResource(R.mipmap.chooce_green);

        }else {
            holder.mCkLayoutItemUseMyhome.setBackgroundResource(R.mipmap.xe_h);
        }
        UIUtils.loadImageView(mContext,userMyHomeListBean.getListUrl(),holder.mIvIconLayoutItemUseMyhome);
        holder.mTvGoodsnameLayoutItemUseMyhome.setText(userMyHomeListBean.getGoodsName());

        String specialSales = userMyHomeListBean.getSpecialSales();


        if (userMyHomeListBean.isAlreadyCustom()){
            //定制
            holder.mLlSpeciallOffterLayoutItemUseMyhome.setVisibility(View.INVISIBLE);
            holder.mLlNomaSalePriceRootLayoutItemUseMyhome.setVisibility(View.VISIBLE);
            holder.mTvSalerNomalPriceLayoutItemUseMyhome.setText("待定价");
            holder.mSelectAttrLayoutItemUseMyhome.setText("私人定制");

        }else {
            //非定制
            holder.mSelectAttrLayoutItemUseMyhome.setText(userMyHomeListBean.getSpecinfo());

            if (StringUtils.isEmpty(specialSales)){
                //非vip
                holder.mLlSpeciallOffterLayoutItemUseMyhome.setVisibility(View.INVISIBLE);
                holder.mLlNomaSalePriceRootLayoutItemUseMyhome.setVisibility(View.VISIBLE);
                holder.mTvSalerNomalPriceLayoutItemUseMyhome.setText("￥"+UIUtils.getMoney(Double.valueOf(userMyHomeListBean.getPrice())));

            }else {
                //vip
                holder.mLlSpeciallOffterLayoutItemUseMyhome.setVisibility(View.VISIBLE);
                holder.mLlNomaSalePriceRootLayoutItemUseMyhome.setVisibility(View.INVISIBLE);
                holder.mTvSpecicalOffterPriceLayoutItemUseMyhome.setText("￥"+UIUtils.getMoney(Double.valueOf(userMyHomeListBean.getSpecialSales())));

                holder.mTvOriginalPriceLayoutItemUseMyhome.setText("￥"+UIUtils.getMoney(Double.valueOf(userMyHomeListBean.getPrice())));
            }

        }



        holder.mNumberButtonLayoutItemUseMyhome.setBuyMax(200).setCurrentNumber(userMyHomeListBean.getNum()).setInventory(200).setOnWarnListener(new NumberButton.OnWarnListener() {
            @Override
            public void onWarningForInventory(int inventory) {

            }

            @Override
            public void onWarningForBuyMax(int max) {

            }
            @Override
            public void number(int number) {
                //  mNofailureBeanList.get(groupPosition).getCart().get(childPosition).setCount(number);

                UserMyHomeNumberEvent  userMyHomeNumberEvent=new UserMyHomeNumberEvent();
                userMyHomeNumberEvent.setNumber(number);
                userMyHomeNumberEvent.setUseMyHomeposition(position);
                EventBus.getDefault().post(userMyHomeNumberEvent);
            }
        });
        holder.mRlCbRootLayoutItemUseMyhome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UseMyHomeCheckPosition useMyHomeCheckPosition=new UseMyHomeCheckPosition();
                boolean check1 = userMyHomeListBean.isCheck();
                if (check1){
                    useMyHomeCheckPosition.setChek(false);

                }else {
                    useMyHomeCheckPosition.setChek(true);

                }

                useMyHomeCheckPosition.setUseMyHomechekPosition(position);
                EventBus.getDefault().post(useMyHomeCheckPosition);

            }
        });
        holder.mRlAttrRoot.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        UseMyHomeAttrEvent useMyHomeAttrEvent=new UseMyHomeAttrEvent();
        useMyHomeAttrEvent.setPostion(position);
        EventBus.getDefault().post(useMyHomeAttrEvent);

    }
});


    }

    @Override
    public int getItemCount() {
        return mUserMyHomeListBeans==null?0:mUserMyHomeListBeans.size();
    }


    class UserMyHomeListBeanViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ck_layout_item_use_myhome)
        CheckBox mCkLayoutItemUseMyhome;
        @BindView(R.id.rl_cb_root_layout_item_use_myhome)
        RelativeLayout mRlCbRootLayoutItemUseMyhome;
        @BindView(R.id.iv_icon_layout_item_use_myhome)
        ImageView mIvIconLayoutItemUseMyhome;
        @BindView(R.id.tv_goodsname_layout_item_use_myhome)
        TextView mTvGoodsnameLayoutItemUseMyhome;
        @BindView(R.id.iv_arrow_layout_item_use_myhome)
        ImageView mIvArrowLayoutItemUseMyhome;
        @BindView(R.id.select_attr_layout_item_use_myhome)
        TextView mSelectAttrLayoutItemUseMyhome;
        @BindView(R.id.tv_specical_offter_price_layout_item_use_myhome)
        TextView mTvSpecicalOffterPriceLayoutItemUseMyhome;
        @BindView(R.id.tv_original_price_layout_item_use_myhome)
        TextView mTvOriginalPriceLayoutItemUseMyhome;
        @BindView(R.id.ll_speciall_offter_layout_item_use_myhome)
        LinearLayout mLlSpeciallOffterLayoutItemUseMyhome;
        @BindView(R.id.tv_saler_nomal_price_layout_item_use_myhome)
        TextView mTvSalerNomalPriceLayoutItemUseMyhome;
        @BindView(R.id.ll_noma_sale_price_root_layout_item_use_myhome)
        LinearLayout mLlNomaSalePriceRootLayoutItemUseMyhome;
        @BindView(R.id.number_button_layout_item_use_myhome)
        NumberButton mNumberButtonLayoutItemUseMyhome;
        @BindView(R.id.ll_root_layout_item_use_myhome)
        LinearLayout mLlRootLayoutItemUseMyhome;
        @BindView(R.id.rl_seletattr_root_layout_item_use_myhome)
        RelativeLayout mRlAttrRoot;

        public UserMyHomeListBeanViewHolder(View itemView) {
            super(itemView);
           // UIUtils.inflate(mContext, R.layout.layout_item_use_myhome);
            ButterKnife.bind(this,itemView);
        }
    }

}
